package com.realife.ysuper.controller;

import com.realife.common.utils.R;
import com.realife.ysuper.entity.CategoryEntity;
import com.realife.ysuper.service.CategoryService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Arrays;
import java.util.List;


/**
 * 商品类目表，类目和商品(spu)是一对多关系，类目与品牌是多对多关系
 *
 * @author Albert
 * @email 15962463559@163.com
 * @date 2020-06-05 10:04:16
 *
 * /ysuper/category/list/tree
 */
@Slf4j
@RestController
@RequestMapping("/ysuper/category")
public class CategoryController {

    @Autowired
    private CategoryService categoryService;


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    //@RequiresPermissions("ysuper:category:info")
    public R info(@PathVariable("id") Long id) {
        CategoryEntity category = categoryService.getById(id);

        return R.ok().put("category", category);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    //@RequiresPermissions("ysuper:category:save")
    public R save(@RequestBody CategoryEntity category) {
        categoryService.save(category);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    //@RequiresPermissions("ysuper:category:update")
    public R update(@RequestBody CategoryEntity category) {
        categoryService.updateById(category);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    //@RequiresPermissions("ysuper:category:delete")
    public R delete(@RequestBody Long[] ids) {
        categoryService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

    @RequestMapping("/list/tree")
    public R list() {

        List<CategoryEntity> entities = this.categoryService.listWithTree();

        return R.ok().put("data", entities);
    }
}
